package cn.itcast.io.a.file;

import java.io.File;

public class FileDemo {
    
    public static final String FILE_SEPARATOR = System.getProperty("file.separator");

    public static void main(String[] args) {
        String pathName = "C:\\Users\\hz21076971\\Desktop\\myJava\\hello.txt";
        File f1 = new File(pathName); // 将文件封装成文件对象
        System.out.println(f1);

        // 这个构造函数的好处是文件夹路径和文件名可以通过传值动态进行改变
        File f2 = new File("C:\\Users\\hz21076971\\Desktop\\myJava", "hello.txt");
        System.out.println(f2);

        // 这个构造函数的好处是当文件目录(文件夹路径)不存在时，可以在程序运行中动态地创建出来
        File dir = new File("C:\\Users\\hz21076971\\Desktop\\myJava");
        File f3 = new File(dir, "hello.txt");
        System.out.println(f3);
        
        // 根据不同平台（Windows Unix），指定文件分隔符
        String tempPathName = "C:\\Users\\hz21076971\\Desktop\\myJava\\hello.txt";
        String pathNameToUse = getPathName(tempPathName);
        File f4 = new File(pathNameToUse);
        System.out.println(f4);
        
        
        
        test1();
        test2();
        test3();

    }

    private static String getPathName(String tempPathName) {
        String osName = System.getProperty("os.name");
        if(osName.toLowerCase().contains("windows")){
            return tempPathName;
        }else {
            //return tempPathName.replace("\\", FILE_SEPARATOR);
            return tempPathName.replace("\\", File.separator);
        }
    }
    
    private static void test1(){
        System.out.println(File.separator);
        System.out.println(File.separatorChar);
        System.out.println(File.pathSeparator);
        System.out.println(File.pathSeparatorChar);
    }
    
    private static void test2(){
        System.out.println(System.getProperty("os.name"));
    }
    
    private static void test3(){
        String osName = "Windows";
        boolean windows = osName.toLowerCase().contains("windows");
        System.out.println(windows);
    }
}
